package com.comeval.persistencia;

import com.comeval.modelo.Persona;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Estudiante
 */
@Stateless
public class PersonaDAO implements IPersonaDAO {

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public void registrarPersona(Persona persona) {
        try {
            entityManager.persist(persona);
        } catch (Exception e) {
            throw e;
        }
    }

    @Override
    public void modificarPersona(Persona persona) {
        try {
            entityManager.merge(persona);
        } catch (Exception e) {
            throw e;
        }
    }

    @Override
    public void eliminarPersona(Persona persona) {
        try {
            entityManager.remove(persona);
        } catch (Exception e) {
            throw e;
        }
    }

    @Override
    public List<Persona> consultarPersonas() {
        try {
            String jpql = "Select persona From Persona persona";
            Query query = entityManager.createQuery(jpql);
            return query.getResultList();
        } catch (Exception e) {
            throw e;
        }
    }

    @Override
    public Persona consultarPorCodigo(String documentonitpersona) {
        try {
            return entityManager.find(Persona.class, documentonitpersona);
        } catch (Exception e) {
            throw e;
        }
    }

}
